<template>
  <div class="employees-detail-container">
    <div class="app-container">
      <el-card>
        <!-- 放置表单 -->
        <el-form
          ref="userForm"
          :model="userInfo"
          :rules="rules"
          label-width="120px"
          style="margin-left: 120px; margin-top: 30px"
        >
          <el-form-item
            label="姓名:"
            prop="username"
          >
            <el-input
              v-model="userInfo.username"
              style="width: 300px"
            />
          </el-form-item>
          <el-form-item label="手机">
            <el-input
              v-model="userInfo.mobile"
              style="width: 300px"
              disabled
            />
          </el-form-item>
          <el-form-item
            label="入职时间"
            prop="timeOfEntry"
          >
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
            />
          </el-form-item>
          <el-form-item label="员工头像">
            <ImageUpload ref="refUpload" />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="updateUserInfo"
            >更新</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getBaseUserInfoApi } from '@/api/user'
import { saveUserDetailByIdApi } from '@/api/employees'
export default {
  name: 'EmployeesDetail',
  data() {
    return {
      userInfo: {
        timeOfEntry: '', // 入职时间
        username: '', // 姓名
        mobile: '', // 手机
        staffPhoto: '' // 头像
      },
      rules: {
        username: [
          {
            required: true,
            message: '用户名不可为空',
            trigger: 'blur'
          }
        ],
        timeOfEntry: [
          {
            required: true,
            message: '入职时间不可为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    userId() {
      return this.$route.params.id
    }
  },
  created() {
    this.getBaseUserInfo()
  },
  methods: {
    updateUserInfo() {
      this.$refs.userForm.validate(async flag => {
        if (!flag) return
        // 方法一
        // let url = ''
        // if (this.$refs.refUpload.fileList.length > 0) {
        //   // 调接口之前，应该获取一下上传的图片地址
        //   url = this.$refs.refUpload.fileList[0].url
        // }
        const refUpload = this.$refs.refUpload
        if (!refUpload.isAllUpload) {
          this.$message.error('图片未上传成功，请稍后再试')
          return
        }
        // 方法二 可选链运算符？. 如果前面的结果为true才会往后走
        const url = this.$refs.refUpload.fileList[0]?.url
        // 调接口之前，应该获取一下上传的图片地址
        this.$refs.refUpload.fileList[0].url
        // 把数据更新到数据库中
        this.userInfo.staffPhoto = url || ''
        await saveUserDetailByIdApi(this.userInfo)
        this.$message.success('修改成功')
      })
    },
    async getBaseUserInfo() {
      const { data } = await getBaseUserInfoApi(this.userId)
      this.userInfo = data
      // this.$refs.refUpload获取上传组件
      this.$refs.refUpload.fileList = [{ url: data.staffPhoto }]
    }
  }
}
</script>
